﻿using UnityEngine;
using UnityEditor;

public class PackerFloderItem : ScriptableObject
{
    public string guid;
}

[CustomEditor(typeof(PackerFloderItem))]
[CanEditMultipleObjects]
public class PackerFolderItemInspector : Editor
{
    FloderPackType GetTargetPackType(Object t)
    {
        var guid = ((PackerFloderItem)t).guid;
        return Packer.setting.GetFloderPackType(guid);
    }

    FloderPackType GetTargetsPackType()
    {
        var pack = GetTargetPackType(target);
        for (int i = 1; i < targets.Length; ++i)
        {
            var p = GetTargetPackType(targets[i]);
            if (pack != p)
            {
                pack = (FloderPackType)(-1);
                break;
            }
        }
        return pack;
    }

    void SetTargetsPackType(FloderPackType pack)
    {
        foreach (var t in targets)
        {
            Packer.setting.SetFloderPackType(((PackerFloderItem)t).guid, pack);
        }
        Packer.Refresh();
    }

    string GetTargetPackFilter(Object t)
    {
        var guid = ((PackerFloderItem)t).guid;
        return Packer.setting.GetFloderPackFilter(guid);
    }

    string GetTargetsPackFilter()
    {
        var filter = GetTargetPackFilter(target);
        for (int i = 1; i < targets.Length; ++i)
        {
            var f = GetTargetPackFilter(targets[i]);
            if (!string.Equals(filter, f))
            {
                filter = "-";
                break;
            }
        }
        return filter;
    }

    void SetTargetsPackFilter(string filter)
    {
        foreach (var t in targets)
        {
            Packer.setting.SetFloderPackFilter(((PackerFloderItem)t).guid, filter);
        }
    }

    string GetTargetPackExtension(Object t)
    {
        var guid = ((PackerFloderItem)t).guid;
        return Packer.setting.GetFloderPackExtension(guid);
    }

    string GetTargetsPackExtension()
    {
        var extension = GetTargetPackExtension(target);
        for (int i = 1; i < targets.Length; ++i)
        {
            var e = GetTargetPackExtension(targets[i]);
            if (!string.Equals(extension, e))
            {
                extension = "-";
                break;
            }
        }
        return extension;
    }
    void SetTargetsPackExtension(string extension)
    {
        foreach (var t in targets)
        {
            Packer.setting.SetFloderPackExtension(((PackerFloderItem)t).guid, extension);
        }
    }

    bool GetTargetPackOnlyPakerUsed()
    {
        var res = GetTargetPackOnlyPakerUsed(target);
        for (int i = 1; i < targets.Length; ++i)
        {
            var e = GetTargetPackOnlyPakerUsed(targets[i]);
            if (!string.Equals(res, e))
            {
                res = false;
                break;
            }
        }
        return res;
    }


    bool GetTargetPackOnlyPakerUsed(Object t)
    {
        var guid = ((PackerFloderItem)t).guid;
        return Packer.setting.GetTargetPackOnlyPackerUsed(guid);
    }

    void SetTargetsPackOnlyPakerUsed(bool res)
    {
        foreach (var t in targets)
        {
            Packer.setting.SetTargetPackOnlyPackerUsed(((PackerFloderItem)t).guid, res);
        }
    }

    bool GetTargetpackerDependencies()
    {
        var res = GetTargetpackerDependencies(target);
        for (int i = 1; i < targets.Length; ++i)
        {
            var e = GetTargetpackerDependencies(targets[i]);
            if (!string.Equals(res, e))
            {
                res = false;
                break;
            }
        }
        return res;
    }


    bool GetTargetpackerDependencies(Object t)
    {
        var guid = ((PackerFloderItem)t).guid;
        return Packer.setting.GetTargetPackpackerDependencies(guid);
    }

    void SetTargetspackerDependencies(bool res)
    {
        foreach (var t in targets)
        {
            Packer.setting.SetTargetPackpackerDependencies(((PackerFloderItem)t).guid, res);
        }
    }

    public override void OnInspectorGUI()
    {
        var pack = GetTargetsPackType();
        var pack2 = (FloderPackType)EditorGUILayout.EnumPopup("Type", pack);
        if (pack != pack2)
            SetTargetsPackType(pack2);

        var filter = GetTargetsPackFilter();
        var filter2 = EditorGUILayout.TextField("Filter", filter);
        if (!string.Equals(filter, filter2))
            SetTargetsPackFilter(filter2);

        var extension = GetTargetsPackExtension();
        var extension2 = EditorGUILayout.TextField("Extension", extension);
        if (!string.Equals(extension, extension2))
            SetTargetsPackExtension(extension2);

        var res = GetTargetPackOnlyPakerUsed();
        var res2 = EditorGUILayout.Toggle("onlyPackerUsed", res);
        if (res != res2)
            SetTargetsPackOnlyPakerUsed(res2);

        var res3 = GetTargetpackerDependencies();
        var res4 = EditorGUILayout.Toggle("packerDependencies", res3);
        if (res3 != res4)
            SetTargetspackerDependencies(res4);

    }
}
